<?php

namespace App\Http\Controllers;
use Hash;
use Illuminate\Http\Request;
use DB;
use Mail;
use App\Http\Requests\HomeLoginPostRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{   
    //后台登录
   public function getIndex()
   {
        //解析模板
        return view('admin.admin.login');
   }

   //后台登录处理
   public function postDologin(Request $request)
   {	
   		// dd($request->all());
   		//检测用户名是否存在
   		$user = DB::table('users')
   			->where('uname',$request->input('uname'))
   			->first();
   		if(!empty($user)){
   			//检测密码是否一致
   			if(Hash::check($request->input('upass'),$user->upass)){
   				//登录成功
   				session(['id'=>$user->id]);
   				//跳转页面
   				return redirect('/admin')->with('success','欢迎管理员'.$user->uname.'登录');
   			}else{
   				return back()->with('error','用户名或密码不正确');
   			}
   		}else{
   			return back()->with('error','用户名或密码不正确');
   		}
   }

   //后台退出
   public function getLogout(Request $request)
   {
   		$request->session()->flush();
   		return ('/admin');
   }

   //前台页面的注册显示
   public function account()
   {
      return view('home.account.account');
   }

   //执行前台注册
   public function doaccount(HomeLoginPostRequest $request)
   {
         // dd($request->all());
         //判断两次密码是否一致
         if($request->input('upass') != $request->input('repass')){
            return back()->with('error','两次输入的密码不一致');
         }

         //提取其它数据
        $data = $request->only('uname','upass','email','phone');

        //数据token
        $data['token'] = str_random(50);

        //密码加密
        $data['upass'] = Hash::make($data['upass']);

        // dd($data);
        // 数据插入
        $id = DB::table('users')->insertGetId($data);

        // 判断
        if($id){ 
            //注册成功 发送激活邮件
            $this->seedmail($id,$data['token'],$request->input('email'));
            return redirect('/account')->with('success','注册成功,激活邮件已发送至您的邮箱,请到邮箱激活');
        }else{
            return back()->with('error','注册失败');
        }
                
   }

   //发送邮箱测试
   public function seedmail($id,$token,$email)
   {
    //     Mail::raw('狗蛋网欢迎你注册请点击.即可获取大量资源...{{$token}}', function ($message) {
    //     //设置邮件标题
    //     $message->subject('激活邮件');
    //     //发送给谁
    //     $message->to($email);
    // });

      Mail::send('email.index', ['id'=>$id,'token'=>$token,'email'=>$email], function ($message)use($email) {
            $message->to($email)->subject('激活邮件');
        });

   }

   //激活账号
   public function jihuo(Request $request)
   {
         //获取id
         $id = $request->input('id');
         //查询
         $user = DB::table('users')->where('id',$id)->first();
         if($user){
               if($request->input('token') == $user->token){
                  $arr = ['status'=>2,'token'=>str_random(50)];  
                  if(DB::table('users')->where('id',$id)->update($arr)){
                     return redirect('/')->with('success','账号激活成功');
                  }else{
                     return redirect('/account')->with('error','账号激活失败');
                  }
               }else{
                  return redirect('/account')->with('error','您的链接已经失效'); 
               }
         }else{
            return redirect('/account')->with('error','没有查询到您的信息'); 
         }

   } 


   //前台登录处理
   public function hologin(Request $request)
   {  
         // dd($request->all());
         //检测用户名是否存在
         $user = DB::table('users')
            ->where('uname',$request->input('uname'))
            ->first();
         if(!empty($user)){
            //检测密码是否一致
            if(Hash::check($request->input('upass'),$user->upass)){
               //登录成功
               session(['id'=>$user->id]);
               //跳转页面
               return redirect('/');
            }else{
               return back()->with('error','用户名或密码不正确');
            }
         }else{
            return back()->with('error','用户名或密码不正确');
         }
   }


}
